﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FaWorks.WebSite.Controllers.Model
{
    public class UserKnowledgeDetailsTypeController : Controller
    {
        #region CRUD
        [HttpGet, ActionName("CURD")]
        public ActionResult HttpGet(Int32? id)
        {
            return this.Read(id);
        }
        [HttpPost, ActionName("CURD")]
        public ActionResult HttpPost(Models.Knowledge.UserKnowledgeDetailsType userKnowledgeDetailsType)
        {
            return this.Create(userKnowledgeDetailsType);
        }
        [HttpPut, ActionName("CURD")]
        public ActionResult HttpPut(Int32 id, Models.Knowledge.UserKnowledgeDetailsType userKnowledgeDetailsType)
        {
            return this.Update(id, userKnowledgeDetailsType);
        }
        [HttpDelete, ActionName("CURD")]
        public ActionResult HttpDelete(Int32 id)
        {
            return this.Delete(id);
        }
        #endregion

        public ActionResult Read(Int32? id)
        {
            if (id != null) {
                return Json(KlDB.UserKnowledgeDetailsTypes.Find(id), JsonRequestBehavior.AllowGet);
            }

            var userID = Int32.Parse(HttpContext.Request.QueryString["userID"]);
            return Json(KlDB.UserKnowledgeDetailsTypes
                .Where(m => m.User_ID == userID)
                .OrderBy(m => (m.ParentCode == null ? "" : m.ParentCode + ".") + m.Code),
                JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create(Models.Knowledge.UserKnowledgeDetailsType userKnowledgeDetailsType)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                KlDB.UserKnowledgeDetailsTypes.Add(userKnowledgeDetailsType);
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Create(userKnowledgeDetailsType);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            ar.Data = userKnowledgeDetailsType.id;
            return Json(ar);
        }

        public ActionResult Update(Int32 id, Models.Knowledge.UserKnowledgeDetailsType userKnowledgeDetailsType)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                var curUserKnowledgeDetailsType = KlDB.UserKnowledgeDetailsTypes.Find(id);
                curUserKnowledgeDetailsType.Name = userKnowledgeDetailsType.Name;
                curUserKnowledgeDetailsType.Code = userKnowledgeDetailsType.Code;
                curUserKnowledgeDetailsType.ParentCode = userKnowledgeDetailsType.ParentCode;
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Update(curUserKnowledgeDetailsType);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        public ActionResult Delete(Int32 id)
        {
            Models.AjaxResult ar = new Models.AjaxResult();
            try
            {
                foreach (Models.Knowledge.Details2UserType d2ut in 
                    KlDB.Details2UserTypes.Where(m => m.UserKnowledgeDetailsType_ID == id))
                {
                    KlDB.Details2UserTypes.Remove(d2ut);
                }

                var userKnowledgeDetailsType = KlDB.UserKnowledgeDetailsTypes.Find(id);
                KlDB.UserKnowledgeDetailsTypes.Remove(userKnowledgeDetailsType);
                KlDB.SaveChanges();

                //Hubs.UserHub.Instance.Clients.All.Delete(curUser);
            }
            catch (Exception ex)
            {
                ar.Success = false;
                ar.Data = ex.Message;
            }

            return Json(ar);
        }

        private KlDataBase KlDB = new KlDataBase();
    }
}
